package com.mc.moring.main;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseActivity;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.base.SaveZiLiPageTool;
import com.mc.moring.bean.GetFullUserBean;
import com.mc.moring.bean.SingleSelectBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.main.adapter.InfoAdapter;
import com.mc.moring.main.modle.MianModle;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.T;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class UserInfoActivity extends BaseActivity {
    MianModle mianModle;
    private ImageButton abBackIBtn;
    private TextView abTitleTV, tv_nombre, tv_father_surname, tv_mother_surname,tv_num, tv_birth, tv_sexo, tv_education, tv_personal_email, tv_existing_loans;
    private TextView tv_companyname, tv_current_profession, tv_payrollcycle, tv_income, tv_payday_1, tv_payday_2;
    private TextView tv_region, tv_city, tv_commune, tv_marital_status;
    private LinearLayout ll_day2;
    private  RecyclerView mRecyclerView;
    private InfoAdapter followAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logE("user info detail page");
        setContentView(R.layout.activity_user_info);
        PointMgr.INSTANCE.sendEvent("cl_person_infoof-1_open");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_person_infoof-1_open");
        abBackIBtn = findViewById(R.id.abBackIBtn);
        abTitleTV = findViewById(R.id.abTitleTV);
        tv_nombre = findViewById(R.id.tv_nombre);
        tv_father_surname = findViewById(R.id.tv_father_surname);
        tv_personal_email = findViewById(R.id.tv_personal_email);
        tv_existing_loans = findViewById(R.id.tv_existing_loans);
        tv_mother_surname = findViewById(R.id.tv_mother_surname);
        tv_num = findViewById(R.id.tv_num);
        tv_education = findViewById(R.id.tv_education);
        tv_sexo = findViewById(R.id.tv_sexo);
        tv_birth = findViewById(R.id.tv_birth);
        tv_companyname = findViewById(R.id.tv_companyname);
        tv_current_profession = findViewById(R.id.tv_current_profession);
        tv_payrollcycle = findViewById(R.id.tv_payrollcycle);
        tv_income = findViewById(R.id.tv_income);
        tv_payday_1 = findViewById(R.id.tv_payday_1);
        tv_payday_2 = findViewById(R.id.tv_payday_2);
        ll_day2 = findViewById(R.id.ll_day2);
        tv_region = findViewById(R.id.tv_region);
        tv_city = findViewById(R.id.tv_city);
        tv_commune = findViewById(R.id.tv_commune);
        tv_marital_status = findViewById(R.id.tv_marital_status);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(UserInfoActivity.this));
        followAdapter = new InfoAdapter();
        mRecyclerView.setAdapter(followAdapter);
        abTitleTV.setText("Informacion personal");
        abBackIBtn.setOnClickListener(v -> finish());
        mianModle = new MianModle();
        ZiLiPageUserDataBean userinfo = new Gson().fromJson(new SaveZiLiPageTool(UserInfoActivity.this).getKeyValue("user_info"), ZiLiPageUserDataBean.class);
        Map<String, String> map = new HashMap<>();
        map.put("custId", userinfo.getCustomerId());
        showLoading();
        mianModle.getUserInfo(map, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                LogUtils.logE("UserInfoActivity request success>" + data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject0 = new JSONObject(data);
                            if (mianModle.isSuccess(jsonObject0)) {
                                JSONObject jsonObject = jsonObject0.optJSONObject("data");
                                tv_nombre.setText(jsonObject.optString("firstName"));
                                tv_father_surname.setText(jsonObject.optString("lastName"));
                                tv_personal_email.setText(jsonObject.optString("email"));
                                if (jsonObject.optString("debt").equals("Y")) {
                                    tv_existing_loans.setText("Sí hay");
                                } else {
                                    tv_existing_loans.setText("No,");
                                }
                                tv_mother_surname.setText(jsonObject.optString("lastMotherName"));
                                tv_num.setText(jsonObject.optString("nationalId"));
                                String education = "";
                                if (jsonObject.optString("educationType").equals("A")) {
                                    education = "Primaria";
                                } else if (jsonObject.optString("educationType").equals("B")) {
                                    education = "Preparatoria";
                                } else if (jsonObject.optString("educationType").equals("C")) {
                                    education = "Licenciatura";
                                } else if (jsonObject.optString("educationType").equals("D")) {
                                    education = "Maestría/Doctorado";
                                } else {
                                    education = "Ninguno";
                                }
                                tv_education.setText(education);
                                if (jsonObject.optString("gender").equals("M")) {
                                    tv_sexo.setText("Masculino");
                                } else {
                                    tv_sexo.setText("Femenino");
                                }
                                Log.e("----birth", jsonObject.optString("birthday"));
                                tv_birth.setText(jsonObject.optString("birthday"));
                                tv_region.setText(jsonObject.optString("province"));
                                tv_city.setText(jsonObject.optString("city"));
                                tv_commune.setText(jsonObject.optString("colony"));
                                String marital_status = jsonObject.optString("maritalStatus");
                                if (marital_status.equals("A")) {
                                    tv_marital_status.setText("Soltero(a)");
                                } else if (marital_status.equals("B")) {
                                    tv_marital_status.setText("Casado(a)");
                                } else if (marital_status.equals("C")) {
                                    tv_marital_status.setText("Viudo(a)");
                                } else if (marital_status.equals("D")) {
                                    tv_marital_status.setText("Separado(a)");
                                } else if (marital_status.equals("E")) {
                                    tv_marital_status.setText("Divorciado(a)");
                                } else {
                                    tv_marital_status.setText("Pareja de hecho(a)");
                                }
                                tv_marital_status = findViewById(R.id.tv_marital_status);

                                JSONObject jobObject = jsonObject.optJSONObject("jobInfo");
                                tv_companyname.setText(jobObject.optString("companyName"));

                                String workStatus = jobObject.optString("workStatus");
                                List<SingleSelectBean> professionList = getProfessionSingleSelectList();
                                if (workStatus.equals("A")) {
                                    tv_current_profession.setText(professionList.get(0).getText());
                                } else if (workStatus.equals("B")) {
                                    tv_current_profession.setText(professionList.get(1).getText());
                                } else if (workStatus.equals("C")) {
                                    tv_current_profession.setText(professionList.get(2).getText());
                                } else if (workStatus.equals("D")) {
                                    tv_current_profession.setText(professionList.get(3).getText());
                                } else if (workStatus.equals("E")) {
                                    tv_current_profession.setText(professionList.get(4).getText());
                                } else if (workStatus.equals("F")) {
                                    tv_current_profession.setText(professionList.get(5).getText());
                                } else if (workStatus.equals("G")) {
                                    tv_current_profession.setText(professionList.get(6).getText());
                                } else if (workStatus.equals("H")) {
                                    tv_current_profession.setText(professionList.get(7).getText());
                                } else if (workStatus.equals("I")) {
                                    tv_current_profession.setText(professionList.get(8).getText());
                                } else if (workStatus.equals("J")) {
                                    tv_current_profession.setText(professionList.get(9).getText());
                                } else if (workStatus.equals("K")) {
                                    tv_current_profession.setText(professionList.get(10).getText());
                                } else if (workStatus.equals("L")) {
                                    tv_current_profession.setText(professionList.get(11).getText());
                                } else if (workStatus.equals("M")) {
                                    tv_current_profession.setText(professionList.get(12).getText());
                                } else if (workStatus.equals("N")) {
                                    tv_current_profession.setText(professionList.get(13).getText());
                                } else {
                                    tv_current_profession.setText(professionList.get(14).getText());
                                }
                                if (jobObject.optString("payrollCycle").equals("0")) {
                                    tv_payrollcycle.setText("Quincenalmente");
                                } else if (jobObject.optString("payrollCycle").equals("1")) {
                                    tv_payrollcycle.setText("Mensualmente");
                                } else if (jobObject.optString("payrollCycle").equals("2")) {
                                    tv_payrollcycle.setText("Semanalmente");
                                } else {
                                    tv_payrollcycle.setText("Otros");
                                }

                                LinkedList<String> stringList = new LinkedList<>();
                                stringList.add("");
                                if (jobObject.optString("income").equals("A")) {
                                    tv_income.setText("Menos que $600.000");
                                } else if (jobObject.optString("income").equals("B")) {
                                    tv_income.setText("$601.000- $900.000");
                                } else if (jobObject.optString("income").equals("C")) {
                                    tv_income.setText("$901.000- $1,200.000");
                                } else if (jobObject.optString("income").equals("D")) {
                                    tv_income.setText("$1.201.000- $1,500.000");
                                } else if (jobObject.optString("income").equals("E")) {
                                    tv_income.setText("$1,501.000 -$1,800.000");
                                }
                                String payDay = jobObject.optString("payDay");
                                if (!TextUtils.isEmpty(payDay)) {
                                    if (payDay.contains(",")) {
                                        String[] payDays = payDay.split(",");
                                        tv_payday_1.setText(payDays[0]);
                                        tv_payday_2.setText(payDays[1]);
                                        ll_day2.setVisibility(View.VISIBLE);
                                    } else {
                                        tv_payday_1.setText(payDay);
                                    }
                                }

                                GetFullUserBean fullUserBean = new Gson().fromJson(data, GetFullUserBean.class);
                                Log.e("----fullUserBean", GsonUtils.INSTANCE.getGson().toJson(data));
                                Log.e("----fullUserBean",String.valueOf(fullUserBean.getData().getInfo()));

                                followAdapter.replaceData(fullUserBean.getData().getInfo());
                            } else {
                                T.showShort(getActivity(), mianModle.getStringToJson(jsonObject0, "msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                hideLoading();
            }
        });
    }

    @Override
    public Activity getActivity() {
        return null;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    public List<SingleSelectBean> getProfessionSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();
        stringList.add("A&Ama de casa");
        stringList.add("B&Estudiante");
        stringList.add("C&Empleado de la ONCE");
        stringList.add("D&Venta ambulante");
        stringList.add("E&Empleado sector agrario");
        stringList.add("F&Contrato fijo (público)");
        stringList.add("G&Contrato temporal (público)");
        stringList.add("H&Contrato fijo (privado)");
        stringList.add("I&Contrato temporal (privado)");
        stringList.add("J&Autónomo");
        stringList.add("K&Prejubilado");
        stringList.add("L&Jubilado");
        stringList.add("M&Pensionista");
        stringList.add("N&Desempleado con prestación");
        stringList.add("O&Desempleado sin prestación");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = stringList.get(i);
            String[] text = s.split("&");
            if (text.length > 1) {
                singleSelectBean.setText(text[1]);
                singleSelectBean.setCode(text[0]);
            } else {
                singleSelectBean.setText(stringList.get(i));
            }
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);

            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    @Override
    public void onStop(){
        super.onStop();
        hideLoading();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PointMgr.INSTANCE.sendEvent("cl_person_infoof-2_open");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_person_infoof-2_open");
    }
}
